package cn.doctorpda.conference.ui.personal.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;

import org.greenrobot.eventbus.EventBus;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.User;
import cn.doctorpda.conference.event.GetMessage;
import cn.doctorpda.conference.localstore.UserInfo;
import cn.doctorpda.conference.presenter.ChangePasswordPresenter;
import cn.doctorpda.conference.utils.MyTextUtil;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.IChangePersonalBaseView;
import okhttp3.Call;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class ChangePswDialogFragment extends DialogFragment implements
        OnClickListener, IChangePersonalBaseView {

    private FragmentActivity mActivity;

    private EditText et_old_psw, et_new_psw;

    private String strOldPSW, strNewPSW;

    private ChangePasswordPresenter changePasswordPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_change_psw, null);

        inflate.findViewById(R.id.btn_submit).setOnClickListener(this);
        inflate.findViewById(R.id.btn_back).setOnClickListener(this);

        et_new_psw = (EditText) inflate.findViewById(R.id.et_new_psw);
        et_old_psw = (EditText) inflate.findViewById(R.id.et_old_psw);

        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        changePasswordPresenter = new ChangePasswordPresenter(this);
        return inflate;
    }

    @Override
    public void onClick(View v) {
//        dismiss();
        switch (v.getId()) {

            case R.id.btn_back:
                this.dismiss();
                break;
            case R.id.btn_submit:
                strNewPSW = et_new_psw.getText().toString().trim();
                strOldPSW = et_old_psw.getText().toString().trim();

                if (strNewPSW.isEmpty() || strOldPSW.isEmpty()) {
                    T.showShort(getContext(), "密码不能为空");
                    break;
                }
                if (!MyTextUtil.checkPassword(strNewPSW) || !MyTextUtil.checkPassword(strOldPSW)) {
                    T.showShort(getContext(), "密码格式不对");
                    break;
                }
                changePasswordPresenter.getNewPSWData(UserInfo.getUserInfo().getMOBILE(), strOldPSW, strNewPSW);
                break;

            default:
                break;
        }
    }


    @Override
    public void setNewView(Msg msg) {

        if (msg.getStatus().equals("success")) {
            this.dismiss();
            T.showShort(getContext(), "密码修改成功");
        } else {
            if (msg.getMsg().contains("用户不存在")) {
                T.showShort(getContext(), "原密码错误");
            } else {
                T.showShort(getContext(), msg.getMsg());
            }
        }
    }

    @Override
    public void setNewError(Call call, Exception e) {

    }
}
